﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RapChieuPhim.BUS;
using DevExpress.XtraEditors;
using RapChieuPhim.DTO;
using System.Collections;
using RapChieuPhim.Properties;
namespace RapChieuPhim.GUI
{
    public partial class BanVeGUI : UserControl
    {

        Button[][] arr;
        int sohang, soday, sogheconlai, maphong, giatien;
        List<String> arrghe = new List<String>();

        public BanVeGUI()
        {
            InitializeComponent();
        }

        private void BanVeGUI_Load(object sender, EventArgs e)
        {
            cbxPhim.Properties.DataSource = PhimBUS.GetAll();
            cbxPhim.Properties.DisplayMember = "TenPhim";
            cbxPhim.Properties.ValueMember = "MaPhim";
        }

        public void LoadGhe()
        {
           
            PhongChieuDTO pc = PhongChieuBUS.GetByID(maphong);
            soday = pc.SoDayGhe;
            sohang = pc.SoHangGhe;

            pncontrol.Size = new Size(soday * 45 + 15, sohang * 30 + 15);
            splitContainerMain_Panel2_Resize(null, null);

            DateTime dt = DateTime.Now;
            int maphim = int.Parse(cbxPhim.EditValue.ToString());
            int masuat = int.Parse(cbxSuat.EditValue.ToString());
            List<String> arrVe = VeBUS.GetAllByGhe(masuat, maphim, dt);

            arr = new Button[soday][];
            for (int i = 0; i < soday; i++)
                arr[i] = new Button[sohang];
            for (int i = 0; i < soday; i++)
                for (int j = 0; j < sohang; j++)
                {
                    arr[i][j] = new Button();
                    arr[i][j].Size = new Size(40, 25);
                    string tenghe = (j + 1).ToString() + "," + (i + 1).ToString();
                    arr[i][j].Text = tenghe;
                    arr[i][j].Location = new Point(i * 45 + 10, j * 30 + 10);
                    arr[i][j].Click += new EventHandler(bt_Click);
                    arr[i][j].BackColor = Color.White;
                    for (int k = 0; k < arrVe.Count; k++)
                    {
                        if (arrVe[k] == tenghe)
                        {
                            arr[i][j].BackColor = Color.Gray;
                            arr[i][j].Enabled = false;
                            arrVe.RemoveAt(k);
                            break;
                        }
                    }
                    pncontrol.Controls.Add(arr[i][j]);
                }
        }

        private void btnChonGhe_Click(object sender, EventArgs e)
        {
            if (txtMaThanhVien.Enabled == true)
            {
                TheThanhVienDTO tv = TheThanhVienBUS.GetByID(txtMaThanhVien.Text);
                if (tv.MaThe == null)
                {
                    XtraMessageBox.Show("Thẻ thành viên không đúng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }
            }
            pncontrol.Visible = true;
            lbGheConLai.Visible = true;
            btnHuy.Visible = true;
            btnChonGhe.Enabled = false;
            pncontrol.Controls.Clear();
            cbxPhim.Enabled = false;
            cbxSuat.Enabled = false;
            cbxPhong.Enabled = false;
            txtChonSoVe.Enabled = false;
            cbxDoiTuong.Enabled = false;
            txtMaThanhVien.Enabled = false;

            sogheconlai = int.Parse(txtChonSoVe.Value.ToString());
            lbGheConLai.Text = "Số ghế còn lại: " + sogheconlai;
            LoadGhe();
        }

        private void bt_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            for (int i = 0; i < soday; i++)
                for (int j = 0; j < sohang; j++)
                {
                    if (arr[i][j].Text == bt.Text)
                    {
                        if (arr[i][j].Tag != null && (int)arr[i][j].Tag == -1)
                        {
                            arr[i][j].BackColor = Color.White;
                            arr[i][j].Tag = null;
                            sogheconlai++;
                            lbGheConLai.Text = "Số ghế còn lại: " + sogheconlai;
                            btnThanhToan.Visible = false;
                            arrghe.Remove(arr[i][j].Text);
                        }
                        else
                        {
                            arrghe.Add(arr[i][j].Text);
                            if (sogheconlai == 1)
                                btnThanhToan.Visible = true;
                            if (sogheconlai == 0)
                            {
                                XtraMessageBox.Show("Bạn đã chọn hết số ghế của mình", "Thông báo");
                                return;
                            }
                            arr[i][j].BackColor = Color.LightGreen;
                            arr[i][j].Tag = -1;
                            sogheconlai--;
                            lbGheConLai.Text = "Số ghế còn lại: " + sogheconlai;
                        }
                        break;
                    }
                }
        }

        private void cbxPhim_EditValueChanged(object sender, EventArgs e)
        {
            cbxPhong.Enabled = true;
            cbxPhong.Reset();
            cbxSuat.Reset();
            cbxPhong.Properties.DataSource = CTPhongChieuBUS.GetAll(int.Parse(cbxPhim.EditValue.ToString()));
            cbxPhong.Properties.DisplayMember = "MaPhong";
            cbxPhong.Properties.ValueMember = "MaPhong";
        }

        private void cbxPhong_EditValueChanged(object sender, EventArgs e)
        {
            cbxSuat.Enabled = true;
            cbxSuat.Reset();
            cbxSuat.Properties.DataSource = SuatChieuBUS.GetByPhong(int.Parse(cbxPhong.EditValue.ToString()));
            cbxSuat.Properties.DisplayMember = "GioBD";
            cbxSuat.Properties.ValueMember = "MaSuatChieu";
            maphong = int.Parse(cbxPhong.EditValue.ToString());

        }

        private void cbxSuat_EditValueChanged(object sender, EventArgs e)
        {
            txtChonSoVe.Enabled = true;


            cbxDoiTuong.Enabled = true;
            cbxDoiTuong.Properties.DataSource = DoiTuongBUS.GetAll();
            cbxDoiTuong.Properties.DisplayMember = "TenDoiTuong";
            cbxDoiTuong.Properties.ValueMember = "MaDoiTuong";


        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            pncontrol.Visible = false;
            lbGheConLai.Visible = false;
            btnHuy.Visible = false;
            cbxPhim.Enabled = true;
            cbxSuat.Enabled = true;
            cbxPhong.Enabled = true;
            txtChonSoVe.Enabled = true;
            btnThanhToan.Visible = false;
            cbxDoiTuong.Enabled = true;
            btnChonGhe.Enabled = false;
            DoiTuongDTO dt = (DoiTuongDTO)cbxDoiTuong.GetSelectedDataRow();
            if (dt.TenDoiTuong == "Thành Viên")
                txtMaThanhVien.Enabled = true;
            else
                txtMaThanhVien.Enabled = false;
            arrghe.Clear();
            sogheconlai = 0;
            giatien = 0;
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {

            SuatChieuDTO suatchieu = (SuatChieuDTO)cbxSuat.GetSelectedDataRow();
            PhimDTO phim = (PhimDTO)cbxPhim.GetSelectedDataRow();
            if (phim.DinhDang3D == true)
                giatien = int.Parse(Settings.Default.GiaTien3D);
            else
                giatien = int.Parse(Settings.Default.GiaTien2D);
            if (suatchieu.CaChieu.MaCaChieu == 4 || suatchieu.CaChieu.MaCaChieu == 5)
                giatien = giatien + (giatien * int.Parse(Settings.Default.GiaCaChieu) / 100);
            DoiTuongDTO dt = (DoiTuongDTO)cbxDoiTuong.GetSelectedDataRow();
            giatien = giatien - (giatien * dt.GiamGia) / 100;

            int tongtien = 0;
            for (int i = 0; i < arrghe.Count; i++)
            {
                VeDTO ve = new VeDTO();
                ve.MaVe = 0;
                ve.DoiTuong = dt;
                ve.GiaTien = giatien;
                ve.Ngaychieu = DateTime.Now;
                ve.Phim = phim;
                ve.SuatChieu = suatchieu;
                if (txtMaThanhVien.Enabled == true)
                {
                    TheThanhVienDTO tv = TheThanhVienBUS.GetByID(txtMaThanhVien.Text);
                    ve.ThanhVien = tv;
                }
                else
                    ve.ThanhVien = null;
                ve.TrangThai = true;
                ve.ViTriGhe = arrghe[i];

                tongtien += giatien;
                VeBUS.Insert(ve);
            }
            XtraMessageBox.Show("Số tiền của bạn phải trả là " + String.Format("{0} đồng", tongtien), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            btnHuy_Click(null, null);
        }

        private void splitContainerMain_Panel2_Resize(object sender, EventArgs e)
        {
            int height = splitContainerMain.Panel2.Height;
            int width = splitContainerMain.Panel2.Width;
            pncontrol.Location = new Point((width - pncontrol.Width) / 2, (height - pncontrol.Height) / 2);
            lbGheConLai.Location = new Point((width - pncontrol.Width) / 2 + 5, (height - pncontrol.Height) / 2-6);
        }

        private void cbxDoiTuong_EditValueChanged(object sender, EventArgs e)
        {
            btnChonGhe.Enabled = true;
            DoiTuongDTO dt = (DoiTuongDTO)cbxDoiTuong.GetSelectedDataRow();
            if (dt.TenDoiTuong == "Thành Viên")
                txtMaThanhVien.Enabled = true;
            else
                txtMaThanhVien.Enabled = false;
        }

        private void txtChonSoVe_EditValueChanged(object sender, EventArgs e)
        {
            sogheconlai = int.Parse(txtChonSoVe.Value.ToString());
        }
    }
}
